set linesize 140
set rmsg on
set scheme s1mono
/******************************************************************************
	Project		:	Army Service in the All-Volunteer Era
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	reduced-form.do
	Description	:	This file runs the reduced-form analysis and creates related
					tables and graphs.
*******************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
*cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"
cap mkdir	"${output}rf/"

*---- select subprograms ----*
local rf_regs		1
local rf_regs_figs	1
local rf_figs		1

*---- useful macros ----*
*lists
local outcomelist	military wages
local stratalist	all white black

*titles
local military_l	"Receives Military W-2"
local wages_l		"Earnings (Dollars)"

*formats
local military_f	"%5.2f"
local wages_f		"%9.0fc"

local keeplist pid access firstafqt *inst* k31* k50* quarter `stratalist'
foreach outcome in `outcomelist'{
	local keeplist "`keeplist' `outcome'_*"
}

*----------------------------------*
*---- reduced-form regressions ----*
*----------------------------------*
if `rf_regs'==1{
	foreach strata of local stratalist{
		foreach outcome of local outcomelist{
		
			*---- initialize output file ----*
			clear
			set obs 22
			gen y = _n - 3
			foreach cut in 31 50 {
				foreach x in b se N m {
					gen `x'_`cut'_rf = .
				}
			}
			save "${output}rf/results_`outcome'_`strata'", replace
			
			*---- loop over years relative to first AFQT ----*
			forval y=-1/19 {
				local yt = `y'
				if `y' == -1 local yt m1

				*---- reduced-form regressions ----*
				use `keeplist' using "${data}army-treasury-analysis" if `strata'==1, clear
					
				*1st cutoff (AFQT=31)
				reg `outcome'_`yt' inst31 k31 k31_2 instk31 instk31_2 ib175.quarter	if inrange(firstafqt,12,49), r
				local b_31_rf = _b[inst31]
				local se_31_rf = _se[inst31]
				local N_31_rf = e(N)
				summ `outcome'_`yt' if e(sample)
				local m_31_rf = r(mean)
					
				*2nd cutoff (AFQT=50)
				reg `outcome'_`yt' inst50 k50 k50_2 instk50 instk50_2 ib175.quarter if inrange(firstafqt,31,68), r
				local b_50_rf = _b[inst50]
				local se_50_rf = _se[inst50]
				local N_50_rf = e(N)
				summ `outcome'_`yt' if e(sample)		
				local m_50_rf = r(mean)
				
				*write results to file
				use "${output}rf/results_`outcome'_`strata'", clear
				foreach cut in 31 50 {
					foreach x in b se N m {
						replace `x'_`cut'_rf = ``x'_`cut'_rf' if y == `y'
					}
				}
				save  "${output}rf/results_`outcome'_`strata'", replace
			}
		}
	}
}	//end if
else di "*---- skipping reduced-form regressions ----*"

*-----------------------------------*
*---- output regression figures ----*
*-----------------------------------*
if `rf_regs_figs'==1{
	foreach strata of local stratalist {
		foreach outcome of local outcomelist {
			*bring in results
			use "${output}rf/results_`outcome'_`strata'", clear
			keep if inrange(y,-1,19)
			
			*create plotting variables
			replace y = y-.1
			gen y2 = y+.2
			foreach cut in 31 50 {
				gen hi_cut`cut'_rf = b_`cut'_rf + 1.96*se_`cut'_rf
				gen lo_cut`cut'_rf = b_`cut'_rf - 1.96*se_`cut'_rf
			}
			
			*plot estimates
			tw 	(rcap hi_cut31_rf lo_cut31_rf y, lcolor(black) lp(dash) lw(thin))	///
				(line b_31_rf y, lcolor(black) lp(dash) lw(medthick))	///
				(rcap hi_cut50_rf lo_cut50_rf y2, lcolor(gs7) lw(thin))	///
				(line b_50_rf y2, lcolor(gs7) lw(medthick))	///
				, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
				ytitle("``outcome'_l'") ylabel(,angle(0) format(``outcome'_f'))	///
				xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
				legend(size(small) order (2 4) label(2 "Cutoff = 31") label(4 "Cutoff = 50"))
			graph export "${output}rf/rf_reg_`outcome'_`strata'.pdf", as(pdf) replace
		}
	}
}	//end if
else di "*---- skip outputting reduced-form regression figures ----*"

*------------------------------*
*---- reduced-form figures ----*
*------------------------------*
if `rf_figs'==1{
	foreach strata of local stratalist{
		foreach outcome of local outcomelist{
			forval y=-1/19 {
				local yt = `y'
				if `y' == -1 local yt m1

				use `keeplist' using "${data}army-treasury-analysis" if `strata'==1, clear
				collapse (mean) `outcome'_`yt', by(firstafqt)
				tw	(qfitci `outcome'_`yt' firstafqt if inrange(firstafqt,12,30)	///
					, clc(black) clw(medthick) fc(gs13) alc(white))	///
					(qfitci `outcome'_`yt' firstafqt if inrange(firstafqt,31,49),	///
					clc(black) clw(medthick) fc(gs13) alc(white))	///
					(qfitci `outcome'_`yt' firstafqt if inrange(firstafqt,50,68),	///
					clc(black) clw(medthick) fc(gs13) alc(white))	///
					(scatter `outcome'_`yt' firstafqt if inrange(firstafqt,12,68),	///
					msiz(medsmall) mc(black) mfc(black) mlw(vvthin)),	///
					xtitle("First AFQT Score on File") xlabel(15(5)65) xmtick(12(1)68)	///
					ytitle("``outcome'_l'") ylabel(, angle(0) format(``outcome'_f'))	///
					xline(30.5 49.5, lcolor(black) lpattern(dot)) legend(off)
				graph export "${output}rf/rf_qfit_`outcome'_`strata'_`yt'.pdf", as(pdf) replace

				use `keeplist' using "${data}army-treasury-analysis" if `strata'==1, clear
				reg `outcome'_`yt' i.quarter if inrange(firstafqt,12,68)
				predict r if inrange(firstafqt,12,68), resid
				sum `outcome'_`yt' if e(sample)
				replace `outcome'_`yt' = r + r(mean) if inrange(firstafqt,12,68)
				collapse (sum) all (mean) `outcome'_`yt', by(firstafqt)
				tw (qfitci `outcome'_`yt' firstafqt if inrange(firstafqt,12,30),	///
					clc(black) clw(medthick) fc(gs13) alc(white))         ///
					(qfitci `outcome'_`yt' firstafqt if inrange(firstafqt,31,49),              ///
					clc(black) clw(medthick) fc(gs13) alc(white))           ///
					(qfitci `outcome'_`yt' firstafqt if inrange(firstafqt,50,68),              ///
					clc(black) clw(medthick) fc(gs13) alc(white))           ///
					(scatter `outcome'_`yt' firstafqt if inrange(firstafqt,12,68),             ///
					msiz(medsmall) mc(black) mfc(black) mlw(vvthin)),           ///
					xtitle("First AFQT Score on File") xlabel(15(5)65) xmtick(12(1)68) ///
					ytitle("``outcome'_l'") ylabel(, angle(0) format(``outcome'_f'))	///
					xline(30.5 49.5, lcolor(black) lpattern(dot)) legend(off)
				graph export "${output}rf/rf_qfit_`outcome'_`strata'_`yt'_demy.pdf", as(pdf) replace
			}
		}
	}
}	//end if
else di "*---- skipping reduced-form figures ----*"

